package com.bytech.comment.biz.controller.emoticon;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bytech.common.biz.enums.emoticon.EmoticonSourceEnum;

import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytech.comment.biz.service.emoticon.EmoticonService;
import com.bytech.common.biz.entity.base.BaseResult;
import com.bytech.common.biz.entity.emoticon.Emoticon;
import com.bytech.common.biz.entity.emoticon.EmoticonDto;
import com.bytech.common.biz.entity.emoticon.EmoticonQuery;

/**
 * @Author Zhao
 * @Create 2021/11/25 23:02
 */
@RestController
@RequestMapping("/emoticon")
public class EmoticonController {

    @Resource
    private EmoticonService emoticonService;

    @GetMapping("/sources")
    public Map<String, String> getEmoticonSources() {
        return Arrays.stream(EmoticonSourceEnum.values()).collect(Collectors.toMap(EmoticonSourceEnum::getCode, EmoticonSourceEnum::getDesc, (k1, k2) -> k2));
    }

    @GetMapping("/getAllUsable")
    public List<Emoticon> getAllUsable(String source) {
        return emoticonService.list(new LambdaQueryWrapper<Emoticon>().eq(Emoticon::getEmoticonFrom, source).eq(Emoticon::getIsUsable, true));
    }

    @PostMapping("/pageByQuery")
    public Page<EmoticonDto> pageByQuery(@RequestBody EmoticonQuery<Emoticon> query) {
        return emoticonService.pageByQuery(query);
    }

    @PostMapping("/deleteById")
    public BaseResult<?> doDeleteEmoticon(@RequestParam("id") Long id) {
        emoticonService.removeById(id);
        return BaseResult.success().msg("删除成功").data(null).build();
    }

    @PostMapping("/toggleUsable")
    public BaseResult<?> doToggleEmoticonUsable(@RequestParam("id") Long id) {
        return emoticonService.toggleUsable(id);
    }

    @PostMapping("/modifyName")
    public BaseResult<?> doModifyEmoticonName(Long id, String name) {
        return emoticonService.doModifyEmoticonName(id, name);
    }

    @PostMapping("/batchAdd")
    public BaseResult<?> doBatchAddEmoticon(@RequestBody List<Emoticon> emoticonList) {
        emoticonService.saveBatch(emoticonList);
        return BaseResult.success("保存成功！").build();
    }
}
